from sklearn.preprocessing import MinMaxScaler
import pandas as pd
import numpy as np


def FEtrain(train):
    # 对类别型特征，观察其取值范围及直方图
    categorical_features = ['season', 'mnth', 'weathersit', 'weekday']

    # 数据类型变为object，才能被get_dummies处理
    for col in categorical_features:
        train[col] = train[col].astype('object')

    X_train_cat = train[categorical_features]
    X_train_cat = pd.get_dummies(X_train_cat)
    X_train_cat.head()
    # 数值型变量预处理，
    # 感觉数据已经做过处理（取值都在0-1之间），这里用MinMaxScaler再处理一次
    mn_X = MinMaxScaler()
    numerical_features = ['temp', 'atemp', 'hum', 'windspeed']
    temp = mn_X.fit_transform(train[numerical_features])

    X_train_num = pd.DataFrame(data=temp, columns=numerical_features, index=train.index)
    X_train_num.head()
    # Join categorical and numerical features
    X_train = pd.concat([X_train_cat, X_train_num, train['holiday'], train['workingday']], axis=1, ignore_index=False)
    X_train.head()
    # 从原始数据中分离输出y
    y = train['cnt']
    # 尝试对y（房屋价格）做log变换，对log变换后的价格进行估计
    log_y = np.log1p(y)
    # 分别初始化对特征和目标值的标准化器
    ss_y = MinMaxScaler()
    ss_log_y = MinMaxScaler()
    y = ss_y.fit_transform(y.values.reshape(-1, 1))
    log_y = ss_y.fit_transform(log_y.values.reshape(-1, 1))

    FE_train = pd.concat([train['instant'], X_train, train['yr']], axis=1)
    FE_train["cnt"] = y
    FE_train.to_csv('FE_day.csv', index=False)
    print(FE_train.head())
    FE_train.info()
